package com.psdi.manager;

import com.psdi.element.*;
import com.psdi.physics.CollisionSystem;
import com.psdi.window.GameJFrame;

import java.util.List;
import java.util.Map;

/**
 * 游戏管理器类
 * 负责游戏的主要逻辑处理和状态管理
 */
public class GameManager {
    public static GameManager gameManager = new GameManager() ;
    private final ElementManager elementManager = ElementManager.getINSTANCE();
    public int killNum = 0;
    public static String level = "第一关";
    public boolean readydoor = true;
    private boolean lock = true;
    public static boolean Gamefinish = false;
    public static boolean victory = false;
    private GameManager(){}


    public void reset(){
        killNum = 0;
        level = "第一关";
        readydoor = true;
        lock = true;
        Gamefinish = false;
        victory = false;
    }
    public String gameprocess(){
        if(killNum>=14 && killNum<62) return"第二波";
        else if (killNum>=62 && level.equals("第一关")) return"第三波";
        else return"第一波";
    }
    public void Detectlevel(){
        CollisionSystem.INSTANCE.detectCollisions();
        if(level.equals("第一关") && killNum <512){
            EnemySystem.getINSTANCE().createEnemys(GameManager.gameManager.gameprocess());
            if(killNum>=126 && readydoor){
                GameLoader.LoadDoor(new Position((float) GameJFrame.GAME_WIDTH /2-35,90));
                readydoor = false;
            }
        }
        else if (level.equals("第二关")) {
            //将除玩家及其属性的所有元素移除,加载新地图
            if(lock){
                clearelements();
                readydoor = true;
                GameLoader.mapLoad("MAP2");
                lock = false;
                EnemySystem.getINSTANCE().createEnemys(level);
                killNum = 0;
            }
            if(!lock){
                EnemySystem.getINSTANCE().createEnemys(GameManager.gameManager.gameprocess()+level);
            }

        }
    }
    public void checkGameconditon(){

        if(!ElementManager.getINSTANCE().getGameElements().get(Tag.BOSS).isEmpty()){
            Boss boss =(Boss) ElementManager.getINSTANCE().getGameElements().get(Tag.BOSS).get(0);
            if(!boss.isAlive()){
                GameLoader.LoadEnd(true);
                Gamefinish = true;
            }
        }
        if(ElementManager.getINSTANCE().getGameElements().get(Tag.PLAYER).isEmpty())
        {
                GameLoader.LoadEnd(false);
                Gamefinish = true;
        }
    }
    private void clearelements(){
        Map<Tag, List<ElementObject>> gameElements = elementManager.getGameElements();
        for (Tag tag : Tag.values()) {
            if (tag.equals(Tag.PLAYER) || tag.equals(Tag.LEVEL)) continue;
            List<ElementObject> elementObjects = gameElements.get(tag);
            for (int i = elementObjects.size() - 1; i >= 0; i--) {
                ElementObject elementObject = elementObjects.get(i);
                elementObject.setAlive(false);
                elementObject.update();
            }
        }
    }
    public void clearallelements(){
        Map<Tag, List<ElementObject>> gameElements = elementManager.getGameElements();
        for (Tag tag : Tag.values()) {
            if (tag.equals(Tag.END)) continue;
            List<ElementObject> elementObjects = gameElements.get(tag);
            for (int i = elementObjects.size() - 1; i >= 0; i--) {
                ElementObject elementObject = elementObjects.get(i);
                elementObject.setAlive(false);
                elementObject.onDestroy();
                elementObjects.remove(i);
            }
        }
    }
}
